package com.ccit.springbootlaw.controller.base;

import com.ccit.springbootlaw.controller.resful.Code;
import com.ccit.springbootlaw.controller.resful.Result;
import com.ccit.springbootlaw.pojo.Subscription;
import com.ccit.springbootlaw.service.SubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/subscriptions")
public class SubscriptionController {

    @Autowired
    private SubscriptionService subscriptionService;

    /**
     * 根据ID查询权益包
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable int id) {
        Subscription subscription = subscriptionService.findById(id);
        if (subscription != null) {
            return new Result(Code.GET_OK, subscription, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "查询失败，未找到对应权益包");
        }
    }

    /**
     * 根据条件查询权益包
     */
    @PostMapping("/search")
    public Result findBySubscriptionWithTypeNameLike(@RequestBody Subscription subscription) {
        List<Subscription> subscriptions = subscriptionService.findBySubscriptionWithTypeNameLike(subscription);
        if (subscriptions != null && !subscriptions.isEmpty()) {
            return new Result(Code.GET_OK, subscriptions, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "查询失败，未找到符合条件的权益包");
        }
    }

    /**
     * 插入新的权益包
     */
    @PostMapping
    public Result insertSubscription(@RequestBody Subscription subscription) {
        int rows = subscriptionService.insertSubscription(subscription);
        if (rows > 0) {
            return new Result(Code.ADD_OK, subscription, "添加成功");
        } else {
            return new Result(Code.ADD_ERR, null, "添加失败");
        }
    }

    /**
     * 更新权益包信息
     */
    @PutMapping
    public Result updateSubscription(@RequestBody Subscription subscription) {
        int rows = subscriptionService.updateSubscription(subscription);
        if (rows > 0) {
            return new Result(Code.UPDATE_OK, subscription, "更新成功");
        } else {
            return new Result(Code.UPDATE_ERR, null, "更新失败");
        }
    }

    /**
     * 删除权益包
     */
    @DeleteMapping("/{id}")
    public Result deleteSubscriptionById(@PathVariable int id) {
        int rows = subscriptionService.deleteSubscriptionById(id);
        if (rows > 0) {
            return new Result(Code.DELETE_OK, null, "删除成功");
        } else {
            return new Result(Code.DELETE_ERR, null, "删除失败");
        }
    }

    /**
     * 查询所有权益包
     */
    @GetMapping
    public Result findAllSubscriptions() {
        List<Subscription> subscriptions = subscriptionService.findAllSubscriptions();
        if (subscriptions != null && !subscriptions.isEmpty()) {
            return new Result(Code.GET_OK, subscriptions, "查询成功");
        } else {
            return new Result(Code.GET_ERR, null, "查询失败，未找到任何权益包");
        }
    }
}